package com.share.shareplatform.sharereward.service.impl;

import com.share.shareplatform.pojo.Reward;
import com.share.shareplatform.sharereward.dao.RewardDao;
import com.share.shareplatform.sharereward.service.RewardService;
import com.share.shareplatform.util.AuthorityUtil;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class RewardServiceImpl implements RewardService {

    @Autowired
    private RewardDao rewardDao;

    @Override
    public Long addOne(Reward reward) {
        return rewardDao.addOne(reward);
    }

    @Override
    public Integer updateBaseOne(Reward reward) {
        return rewardDao.updateBaseOne(reward);
    }

    @Override
    public Integer deleteAllByIds(List<Long> ids) {

        return rewardDao.deleteAllByIds(ids);
    }

    @Override
    public Reward findOneById(Long id) {
        return rewardDao.findOneById(id);
    }

    @Override
    public List<Reward> findPage(Integer pageIndex, Integer pageSize) {
        return rewardDao.findPage((pageIndex-1)*pageSize,pageSize);
    }

    @Override
    public List<Reward> findAllByIds(List<Long> ids) {
        return rewardDao.findAllByIds(ids);
    }

    @Override
    public List<Reward> findAllByUserId(Long id) {
        return rewardDao.findAllByUserId(id);
    }

    @Override
    public List<Reward> findAll() {
        return rewardDao.findAll();
    }

    /*
    *
    * 计算经纬度之间的距离
    *
    * */
    @Override
    public List<Reward> findAllByDistance(Reward reward) {
        /*List<Reward> list = rewardDao.findAll();
        GlobalCoordinates source = new GlobalCoordinates(reward.getLatitude(), reward.getLongitude());
        GlobalCoordinates listReward=new GlobalCoordinates(0,0);
        List<Reward> resultList=new ArrayList<>();
        for(Reward r:list){
            listReward.setLatitude(r.getLatitude());
            listReward.setLongitude(r.getLongitude());
            int distance=(int)getDistanceMeter(source,listReward,Ellipsoid.WGS84);
            System.out.println("距离："+distance);
            r.setDistance(distance);
            if(distance<Reward.MAX_DISTANCE){
                resultList.add(r);
            }
        }
        resultList.sort((o1,  o2)-> {
            if(o1.getDistance()>=o2.getDistance()){
                return 1;
            }
            return -1;
        });
        return resultList;*/
        return null;
    }

    /*public static void main(String[] args){

        GlobalCoordinates source = new GlobalCoordinates(29.490295, 106.486654);
        GlobalCoordinates target = new GlobalCoordinates(29.615467, 106.581515);

        double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
        double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);

        System.out.println("Sphere坐标系计算结果："+meter1 + "米");
        System.out.println("WGS84坐标系计算结果："+meter2 + "米");
    }*/

    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){

        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);

        return geoCurve.getEllipsoidalDistance();
    }
}
